﻿--#include "data\config\welfare\SevenDayLoginAwardCfg.lua" once
--七天登陆
MiscsImplement.GetSevenDayLoginAward = function( sysarg, msgid, packet )

	local giveIndex = DataPack.readByte(packet)
	
	if not SevenDayAwardCfg then return end

	local config = SevenDayAwardCfg[giveIndex]

	if not config then return end
		--当前等级不满足领取条件
	if Actor.checkActorLevel(sysarg,SevenDayAwardCfg.level) == false then
		Actor.sendTipmsg(sysarg,string.format(Lang.ScriptTips.zf198,SevenDayAwardCfg.level),ttFlyTip)
		return
	end

	if Actor.checkActorLevel(sysarg,config.getLevel) == false then
		Actor.sendTipmsg(sysarg,string.format(Lang.ScriptTips.zf198,config.getLevel),ttFlyTip)
		return
	end
	
	local var = Actor.getStaticVar(sysarg)
	if not var.SevenDayFlag then
		return
	elseif config.id > var.SevenDayFlag then
		Actor.sendTipmsg(sysarg,string.format(Lang.ScriptTips.zf195,config.id),ttFlyTip) 	--需要累积多少天才可领取
		return
	else
		if not var.SevenDayAwardFlag then
			var.SevenDayAwardFlag = {}
		elseif var.SevenDayAwardFlag[giveIndex] then
			Actor.sendTipmsg(sysarg,Lang.ScriptTips.zf193,ttFlyTip)		--已经领取过该奖励了，不能再重复领取
			return
		end
		
		if CheckBagGridForAwardsEx(sysarg,config.award) == 0 then
			GiveCommonAwardEx(sysarg,config.award,998,Lang.LogTips.SevenDaysLoginGift)
			Actor.sendTipmsg(sysarg,Lang.ScriptTips.zf194,ttFlyTip)
			var.SevenDayAwardFlag[giveIndex] = 1
			--System.broadcastTipmsg(string.format(Lang.ScriptTips.zf219,Actor.getName(sysarg),giveIndex),128+2)

			-- 记录日志
			local nActorId = Actor.getIntProperty(sysarg, PROP_ENTITY_ID)
			System.sendCommonLog(GameLog.cGetSevenDayLogin,nActorId,'',config.id,0,0,'','','','','','','',"Get Seven Day LoginAward")
		else
			Actor.sendTipmsg(sysarg,Lang.ScriptTips.COM0015,ttFlyTip)
		end
		
		AllLoginAwardInfo(sysarg)
	end
end

MiscsImplement.GetSevenDayLoginAwardInfo = function( sysarg, msgid, packet )
	AllLoginAwardInfo(sysarg)
end

--更新玩家累计登录多少天
function AddLoginAwardInfo(sysarg)
	local var = Actor.getStaticVar(sysarg);
	if not var.SevenDayFlag then
		var.SevenDayFlag = 1
	else
		var.SevenDayFlag = var.SevenDayFlag + 1
	end
end

-- 所有登录奖励信息
function AllLoginAwardInfo(sysarg)
	local var = Actor.getStaticVar(sysarg);
	local SevenDayAwardFlag
	if not var.SevenDayAwardFlag then
		var.SevenDayAwardFlag = {}
	end
	if not var.SevenDayFlag then
		var.SevenDayFlag = 1
	end
	SevenDayAwardFlag = var.SevenDayAwardFlag
	
	local npack = DataPack.allocPacket(sysarg,LogicCmd.miscSys.sysId, LogicCmd.miscSys.sub.sSendLoginAwardInfo)  --申请一个数据包
	DataPack.writeByte(npack,var.SevenDayFlag)
	DataPack.writeByte(npack,#SevenDayAwardCfg)
	for i = 1, table.getn(SevenDayAwardCfg) do
		if SevenDayAwardFlag[i] then
			DataPack.writeByte(npack,2)  --已领取
		elseif SevenDayAwardCfg[i].id > var.SevenDayFlag then
			DataPack.writeByte(npack,0)	 --不能领取
		else
			DataPack.writeByte(npack,1)   --可以领取
		end
	end
	DataPack.flush(npack);
end


MiscsImplement.registerMsgHandler(LogicCmd.miscSys.sub.cGetSevenDayLoginAward, MiscsImplement.GetSevenDayLoginAward)
MiscsImplement.registerMsgHandler(LogicCmd.miscSys.sub.cGetSevenDayLoginAwardInfo, MiscsImplement.GetSevenDayLoginAwardInfo)